<?php

/**
 * @copyright (C)2016-2099 Hnaoyun Inc.
 * @author XingMeng
 * @email hnxsh@foxmail.com
 * @date  2017年12月15日
 *  文章控制器
 */

namespace app\admin\controller\content;

use core\basic\Controller;
use app\admin\model\content\ContentModel;

class ContentBellController extends Controller {

    private $model;
    private $blank;

    public function __construct() {
        $this->model = new ContentModel();
    }

    // 文章列表
    public function index() {
        $this->assign('list', true);
        $result = $this->model->getListBellAll();
        $this->assign('contents', $result);
        $this->assign('baidu_zz_token', $this->config('baidu_zz_token'));
        $this->assign('baidu_xzh_appid', $this->config('baidu_xzh_appid'));
        $this->assign('baidu_xzh_token', $this->config('baidu_xzh_token'));

        // 前端地址连接符判断
        $url_break_char = $this->config('url_break_char') ?: '_';
        $this->assign('url_break_char', $url_break_char);
        $this->display('content/contentbell.html');
    }
    // 文章修改
    public function mod()
    {
        if (! $id = get('id', 'int')) {
            error('传递的参数值错误！', - 1);
        }
        
        // 单独修改状态
        if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
            if ($this->model->modContent($id, "$field='$value',update_user='" . session('username') . "'")) {
                location(- 1);
            } else {
                alert_back('修改失败！');
            }
        }
    }
    
    /**
     * 批量审核文章
     */
    public function BatchChangeStatus(){
         // 文章删除
        if ($_POST) {
            if ($list = post('list')) {
                if ($this->model->modContent($list, "status=1,update_user='" . session('username') . "'")) {
                    success('批量审核成功！', - 1);
                } else {
                    error('批量审核失败！', - 1);
                }
            } else {
                alert_back('请选择要审核的内容！');
            }
        }else{
            alert_back('请求方式错误');
        }
    }
    // 生成分类选择
    private function makeSortSelect($tree, $selectid = null) {
        $list_html = '';
        foreach ($tree as $value) {
            // 默认选择项
            if ($selectid == $value->scode) {
                $select = "selected='selected'";
            } else {
                $select = '';
            }
            $list_html .= "<option value='{$value->scode}' $select>{$this->blank}{$value->name}";
            // 子菜单处理
            if ($value->son) {
                $this->blank .= '　　';
                $list_html .= $this->makeSortSelect($value->son, $selectid);
            }
        }
        // 循环完后回归位置
        $this->blank = substr($this->blank, 0, - 6);
        return $list_html;
    }

}
